import AJAX from '@/common/ajax'

export const GET_NEWS_CONTENT = 'GET_NEWS_CONTENT'
export const GET_NEWS_LIST = 'GET_NEWS_LIST'

export default {
  namespaced: true,
  state: {
    newsContent: {},
    newsList: {}
  },
  mutations: {
    [GET_NEWS_CONTENT](state, { data, params }) {
      state.newsContent[params.id] = data
      console.log('state.newsContent', state.newsContent)
    },
    [GET_NEWS_LIST](state, { data, params }) {
      state.newsList[`${params.id}${params.p ? params.p : ''}`] = data
      console.log('state.newsList', state.newsList, params)
    }
  },
  actions: {
    async getNewsContent({ commit, state }, params) {
      const data = state.newsContent[params.id]
      if (data) {
        return data
      } else {
        let [err, data] = await AJAX({
          name: 'newsDetail',
          data: {
            id: params.id
          }
        })
        commit(GET_NEWS_CONTENT, {
          data,
          params
        })
        return data
      }
    },
    async getNewsList({ commit, state }, params) {
      const data = state.newsList[`${params.id}${params.p ? params.p : ''}`]
      if (data) {
        return data
      } else {
        let [err, data] = await AJAX({
          name: 'newslist',
          data: {
            id: params.id,
            p: params.p || 0
          }
        })
        commit(GET_NEWS_LIST, {
          data,
          params
        })
        return data
      }
    }
  }
}
